package com.gotokeep.keep.composition.renderer;

import android.annotation.TargetApi;
import android.media.MediaCodec;
import android.media.MediaCodecInfo;
import android.media.MediaFormat;
import android.os.Build;
import android.util.Log;
import android.view.Surface;

import com.gotokeep.keep.composition.MediaExportConfiguration;
import com.gotokeep.keep.composition.RenderNode;
import com.gotokeep.keep.composition.RenderSource;

import java.io.IOException;

/**
 * Render contains {@link RenderSource}, {@link RenderNode}
 * @author xana/cuixianming
 * @version 1.0
 * @since 2018-05-04 16:46
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public class VideoMediaRenderer extends MediaRenderer {
    private Surface outputSurface = null;

    @Override
    public void prepareEncoder(MediaExportConfiguration configuration) throws IOException {
        super.prepareEncoder(configuration);

        MediaFormat videoFormat = MediaFormat.createVideoFormat(configuration.getVideoMimeType(),
                configuration.getWidth(), configuration.getHeight());
        videoFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT,
                MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
        videoFormat.setInteger(MediaFormat.KEY_BIT_RATE, configuration.getVideoBitRate());
        videoFormat.setInteger(MediaFormat.KEY_FRAME_RATE, configuration.getFrameRate());
        videoFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, configuration.getKeyFrameInterval());
        if (verbose) {
            Log.v(TAG, "Encoder video format: " + videoFormat);
        }

        encoder = MediaCodec.createEncoderByType(configuration.getVideoMimeType());
        encoder.configure(videoFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
        if (outputSurface != null) {
            outputSurface.release();
        }
        outputSurface = encoder.createInputSurface();
        encoder.start();
    }

    @Override
    public long render(long presentationTimeUs) {
        return 0;
    }
}
